package com.maple.base.model

import com.blankj.utilcode.util.GsonUtils
import com.maple.model.resp.BaseResp
import com.maple.utils.util.EncryptDecodeUtils
import com.maple.utils.util.LogUtils
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.util.*


abstract class BaseRepository {

    abstract fun getPublicParams(): WeakHashMap<String, Any>

    fun params2Body(params: WeakHashMap<String, Any>, hasToken: Boolean = true): RequestBody {
        //添加公共请求参数
        if(hasToken){
            params.putAll(getPublicParams())
        }
        LogUtils.logGGQ("-------参数明细---------")
        params.forEach {
            LogUtils.logGGQ("提交参数：：->>>${it.key} = ${GsonUtils.toJson(it.value)}")
        }
        LogUtils.logGGQ("----共：${params.size}----")
        LogUtils.logGGQ("--json:-->${GsonUtils.toJson(params)}")
        return JSONObject(params.toMap()).toString().toRequestBody("application/json;charset=utf-8".toMediaType())
    }

    fun params2Json(json: String): RequestBody{
        return JSONObject(json).toString().toRequestBody("application/json;charset=utf-8".toMediaType())
    }

    fun params2Encrypt(params: WeakHashMap<String, Any>): RequestBody{
        val encrypt = EncryptDecodeUtils.encrypt(JSONObject(params.toMap()).toString())
        LogUtils.logGGQ("====encrypt===>>${encrypt}")
        return encrypt.toRequestBody("application/json;charset=utf-8".toMediaType())
    }
}